package com.ewhale.feitengguest.ui.user.message;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.app.AppApplication;
import com.ewhale.feitengguest.dto.HaveUnreadMsgDto;
import com.ewhale.feitengguest.presenter.user.message.MessagePresenter;
import com.ewhale.feitengguest.view.user.message.MessageView;
import com.flyco.tablayout.SlidingTabLayout;
import com.hyphenate.chat.EMClient;
import com.simga.library.activity.MBaseFragment;

import java.util.ArrayList;

import butterknife.BindView;

public class MessageFragment extends MBaseFragment<MessagePresenter> implements MessageView {


    @BindView(R.id.acty_main_tab)
    SlidingTabLayout mActyMainTab;
    @BindView(R.id.acty_main_vp)
    ViewPager mActyMainVp;
    @BindView(R.id.tv_service)
    TextView mTvService;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private MyPagerAdapter mAdapter;
    private final String[] mTitles = {"消息公告", "师徒关系"};
    private Fragment nowShowFragment;

    @Override
    protected int getViewId() {
        return R.layout.fragment_message;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        registerReceiver();
        mFragments.add(new NewsBulletinFragment());
        mFragments.add(new ChatConversationFragment());
        mAdapter = new MyPagerAdapter(getChildFragmentManager());
        mActyMainVp.setAdapter(mAdapter);
        mActyMainTab.setViewPager(mActyMainVp);
        nowShowFragment = mFragments.get(0);
        if (AppApplication.Jump_invite_list) {
            mActyMainTab.setCurrentTab(1);
            AppApplication.Jump_invite_list = false;
        }
//        if (EMClient.getInstance().chatManager().getUnreadMessageCount() > 0) {
//            mActyMainTab.showMsg(1, EMClient.getInstance().chatManager().getUnreadMessageCount());
//        }
        presenter.getMessageUnRead();
    }

    @Override
    protected void initListener() {
        mActyMainVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                nowShowFragment = mFragments.get(position);
                mActyMainTab.hideMsg(0);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
//        mTvService.setOnClickListener(v -> BizChatActivity.open(mContext));
    }

    @Override
    public void showProLoading() {
    }

    @Override
    public void dimissProLoading() {
    }

    @Override
    public void showErrorMessage(int postCode, String code, String message) {
    }

    @Override
    public void haveMessageUnRead(HaveUnreadMsgDto haveUnreadMsgDto) {
        if(haveUnreadMsgDto.getHaveUnreadMsg() == 1){
            mActyMainTab.showDot(0);
        }else{
            mActyMainTab.hideMsg(0);
        }
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {//界面显示
            if (nowShowFragment != null) {
                if (nowShowFragment instanceof NewsBulletinFragment) {
                    presenter.getMessageUnRead();
//                    ((NewsBulletinFragment) nowShowFragment).requestList(1);
                }
//                else if (nowShowFragment instanceof ChatConversationFragment) {
//                    ((ChatConversationFragment) nowShowFragment).requestConversion();
//                }
            }
        }
    }


    private void registerReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction("JUMP_INVITE_LIST");
//        filter.addAction("UPDATA_MESSAGE");
        getActivity().registerReceiver(myReceiver, filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(myReceiver);
    }

    private BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("JUMP_INVITE_LIST")) {
                mActyMainTab.setCurrentTab(1);
                AppApplication.Jump_invite_list = false;
            }
//            else if (intent.getAction().equals("UPDATA_MESSAGE")) {
//                mActyMainTab.showMsg(1, EMClient.getInstance().chatManager().getUnreadMessageCount());
//            }
        }
    };
}
